#include<bits/stdc++.h>
using namespace std;
const int N = 4e5+7;
char ori[3][3], buf[3][3], dst[3][3];
typedef long long ll;
ll que[N];
int hh, tt;
int D[N];
char xpos[N][2];
unordered_set<ll> s;
#define format for(int i=0; i<3;++i)for(int j=0; j<3; ++j)
int ox, oy;
const int dx[] = {1, 0, -1, 0}, dy[]={0, 1, 0, -1};
ll encode(char mat[3][3]) {
  ll x = 0;
  format {
    x *= 10; x += mat[i][j];
  }
  return x;
}
void decode(ll val) {
  for(int i=2; i>=0; i--) {
    for(int j=2; j>=0; j--) {
      buf[i][j] = val%10;
      val/=10;
    }
  }
}
void push(ll val, char x, char y, int dep) {
  s.insert(val);
  que[tt] = val;
  xpos[tt][0]=x, xpos[tt][1]=y;
  D[tt] = dep;
  tt++;
}
bool check(int x, int y) {
  return !(x<0||y<0||x>=3||y>=3);
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif
  format {
    char c; cin>>c;
    if(c=='x') {
      c='0';
      ox = i, oy = j;
    }
    ori[i][j] = c-'0';
  }
  push(encode(ori), ox, oy, 1);
  while(hh<tt) {
    int dep = D[hh];
    decode(que[hh]);
    int x = xpos[hh][0], y=xpos[hh][1];
    hh++;
    for(int i=0; i<4; ++i) {
      int nx = x+dx[i], ny=y+dy[i];
      if(!check(nx, ny)) continue;
      swap(buf[x][y], buf[nx][ny]);
      ll code = encode(buf);
      swap(buf[x][y], buf[nx][ny]);
      if(s.count(code)) {
        continue;
      }
      if(code == 123456780LL) {
        cout<<dep<<'\n';
        return 0;
      }
      push(code, nx, ny, dep+1);
    }
  }
  cout<<"-1\n";
}
